package ai4games_tests.maths;

import game2dai.maths.Geometry2D;
import processing.core.PApplet;

public class CircleCircleIntersection extends PApplet {

	final int IRAD = 5;
	float cx0,cy0,r0,cx1,cy1,r1;
	float[] ipoint = null;
	boolean intersect;

	public void setup(){
		size(400,300);
		cursor(CROSS);
		cx0 = width/2;
		cy0 = height/2;
		r0 = 30;
		r1 = 50;
		cx1 = cx0 + r0 + r1;
		cy1 = cy0;
		intersect = false;
		ellipseMode(RADIUS);
	}

	public void draw(){
		background(128);
		cx1 = mouseX;
		cy1 = mouseY;
		intersect = Geometry2D.circle_circle(cx0,cy0,r0,cx1,cy1,r1);
		ipoint = makeFloatArray(Geometry2D.circle_circle_p(cx0,cy0,r0,cx1,cy1,r1));
		strokeWeight(1.1f);
		stroke(0,255,0);
		fill(255,255,192,140);
		if(intersect)
			stroke(255,0,0);
		ellipse(cx0,cy0,r0,r0);
		ellipse(cx1,cy1,r1,r1);

		fill(200,200,255);
		noStroke();
		for(int p = 0; p < ipoint.length; p += 2)
			ellipse((float)ipoint[p], (float)ipoint[p+1], IRAD,IRAD);
		
		fill(255,255,0);
		text("Number of intersections = " + ipoint.length/2, 10, height - 4);
	}

	public float[] makeFloatArray(double[] da){
		float[] fa = new float[da.length];
		for(int i = 0; i < fa.length; i++)
			fa[i] = (float)da[i];
		return fa;
	}

}
